﻿
package org.flashbrighton.as3bots.effects {
	import flash.display.*
	import flash.events.Event;
	
/**
	 * ...
	 * @author Jaime Domínguez
	 */
	
	public class Explosion extends Sprite {
		
		private var numberOfLines:int = 1;
		private var blurStrength:int = 1;
		private var linesColour:Number;
		private var _numFinished:int = 0;
		
		public function Explosion(strength:Number,colour:Number) {
			init(strength,colour);
		}
		
		public function init(strength:Number,colour:Number):void {
			initVars(strength,colour);
			explode(strength);
		}
		
		public function explode(strength:Number):void {
			//separation in degrees.
			var linesSpacing:Number = 360 / numberOfLines;
			linesSpacing = Math.floor(linesSpacing);
			
			//initial degrees to start with. (makes explosions to have all different angles)
			var startingPosition:int =  Math.floor(Math.random() * 90);
			
			for (var i:int = 0; i < numberOfLines; i++) {
				var rotation:int = startingPosition + (i * linesSpacing);
				var speed:Number = Math.random() * strength;
				var particle:LineParticle = new LineParticle(rotation,linesColour,speed);
				var particle2:LineParticle = new LineParticle(rotation,0xffffff,speed);
				particle.addEventListener(Event.REMOVED, destroy);
				particle2.addEventListener(Event.REMOVED, destroy);
				this.addChild(particle);
				this.addChild(particle2);
			}
		}
		
		private function destroy(e:Event):void 
		{
			_numFinished++;
			if (_numFinished == numberOfLines * 2)
			{
				this.parent.removeChild(this);
				//dispatchEvent(new Event(Event.COMPLETE));
			}
		}
	
		
		public function initVars(strength:Number, colour:Number):void {
			
			numberOfLines = strength*2;
			blurStrength = strength;
			linesColour = colour;
		}
		
		
		
		
	}

}